import React, { useState } from 'react';


//hooks
function Example(){
    let  [ count, setCount] = useState(0)
    // let _userState = useState(0) //数组解构赋值，useState(0)为一个数组
    // let count = _userState[0]
    // let setCount = _userState[1]
    //通过顺序记录的状态，useState必须放在function的根位置，不能放在if判断中
    return(
        <div>
            <p>You clicked {count} times</p>
            <button onClick={()=>{setCount(++ count)}}>点击</button>
        </div>
    )
}
//原始
// class Example extends Component {
//     constructor(props) {
//         super(props);
//         this.state = { count:0 }
//     }
//     render() { 
//         return ( 
//             <div>
//                 <p>You clicked {this.state.count} times</p>
//                 <button onClick={this.addCount.bind(this)}>点击</button>
//             </div>
//          );
//     }
//     addCount(){
//         this.setState({count:++this.state.count})
//     }
// }
 
export default Example;